//
//  RedUnreadView.swift
//  test-ios
//
//  Created by wyz5204 on 2023/7/17.
//

import UIKit

class RedUnreadView : UIAbstractView {
    
    private lazy var unReadLabel = {
        return UILabel()
    }()
    
    var num : Int = 0 {
        didSet {
            defaultLayout(num)
        }
    }
    
    override func initView(_ frame: CGRect) {
        /// 此处UI 修改过 首页样式
        unReadLabel.font = UIFont.systemFont(ofSize: 10)
        unReadLabel.textColor = .white;
        unReadLabel.setTitleAndColor("000")
        unReadLabel.textAlignment = .center;
        unReadLabel.sizeToFit()
        addSubview(unReadLabel)
        self.backgroundColor = .red
        
        defaultLayout(num)
    }
    
    func defaultLayout(_ num : Int){
        self.isHidden = num == 0;
        
        var unReadStr = String(num)
        if (num > 99){
            unReadStr = "99+";
        }
        unReadLabel.setTitleAndColor(unReadStr , color: .white)
        unReadLabel.sizeToFit()
        var width = unReadLabel.frame.size.width + 2 * 4;
        let height = unReadLabel.frame.size.height + 2 * 2;
        if(width < height){
            width = height;
        }
        self.frame = CGRect(x: 0, y: 0, width: width, height: height)
        unReadLabel.frame = self.bounds;
        
        self.layer.cornerRadius = (unReadLabel.frame.size.height)/2.0
        self.layer.masksToBounds = true
        
        self.layoutIfNeeded()
    }
}
